package com.service.impl;

import com.dao.AdminDao;
import com.dto.BaseDto;
import com.dto.MessageEnum;
import com.entity.Admin;
import com.page.PageInfo;
import com.service.AdminService;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Service("adminService")
public class AdminServiceImpl implements AdminService {
    @Resource
    AdminDao adminDao;
    public Admin getByAdid(String adid){
      return adminDao.getByAdid(adid);
    }
    //业务逻辑层，根据业务逻辑确定返回给页面的消息和状态以及对象
    public BaseDto<Admin> getAll(){
        List<Admin> admins = adminDao.getAll();
        //通过判断dao层返回的对象的集合大小来说明本次查询是成功还是失败 如果大于0成功有返回的结果集，否则就没有查到东西就是失败
        MessageEnum messageEnum = admins.size() > 0?MessageEnum.SELECT_ADMINBYPAGE_SUCCESS:MessageEnum.SELECT_ADMINBYPAGE_FAIL;
        BaseDto<Admin> dto = new BaseDto<>(messageEnum,admins);
        return dto;
    }

    public BaseDto<Admin> getAllByPage(PageInfo pageInfo){
        List<Admin> admins = adminDao.getAllByPage(pageInfo);
        //通过判断dao层返回的对象的集合大小来说明本次查询是成功还是失败 如果大于0成功有返回的结果集，否则就没有查到东西就是失败
        MessageEnum messageEnum = admins.size() > 0?MessageEnum.SELECT_ADMINBYPAGE_SUCCESS:MessageEnum.SELECT_ADMINBYPAGE_FAIL;
        BaseDto<Admin> dto = new BaseDto<>(messageEnum,admins);
        dto.setObj(pageInfo);
        return dto;
    }


    public BaseDto<Admin> searchNameByPage(PageInfo pageInfo,String adname){
        List<Admin> admins = adminDao.searchNameByPage(pageInfo,"%"+adname+"%");
        //通过判断dao层返回的对象的集合大小来说明本次查询是成功还是失败 如果大于0成功有返回的结果集，否则就没有查到东西就是失败
        MessageEnum messageEnum = admins.size() > 0?MessageEnum.SEARCHADMIN_SUCCESS:MessageEnum.SEARCHADMIN_FAIL;
        BaseDto<Admin> dto = new BaseDto<>(messageEnum,admins);
        dto.setObj(pageInfo);
        return dto;
    }

    /**
     * 根据主键查询
     * @param adid
     * @return
     */
    public BaseDto getAdminByAdid(String adid){
        Admin admin = adminDao.getAdminByAdid(adid);
        MessageEnum messageEnum = Optional.ofNullable(admin).isPresent()?MessageEnum.SELECT_ADMINBYPK_SUCCESS:MessageEnum.SELECT_ADMINBYPK_FAIL;
        BaseDto dto = new BaseDto(messageEnum);
        dto.setT(admin);
        return dto;
    }

    /**
     * 添加一个实体
     * @param admin
     * @return
     */
    public BaseDto addAdmin(Admin admin){
        int result = adminDao.addAdmin(admin);
        MessageEnum messageEnum = result>0?MessageEnum.ADDADMIN_SUCCESS:MessageEnum.ADDADMIN_FAIL;
        BaseDto dto = new BaseDto(messageEnum);
        dto.setT(admin);
        return dto;
    }
    /**
     * 修改实体
     */
    public BaseDto updateAdmin(Admin admin){
        int result = adminDao.updateAdmin(admin);
        MessageEnum messageEnum = result>0?MessageEnum.UPDATEADMINBYPK_SUCCESS:MessageEnum.UPDATEADMINBYPK_FAIL;
        BaseDto dto = new BaseDto(messageEnum);
        dto.setT(admin);
        return dto;
    }
    public BaseDto updateAdminByExample(){
        return null;
    }

    /**
     * 删除一个实体
     * @param adid
     * @return
     */
    public BaseDto deleteAdminByPrimayKey(String adid){
        int result = adminDao.deleteAdminByPrimayKey(adid);
        MessageEnum messageEnum = result>0?MessageEnum.DELETEADMINBYPK_SUCCESS:MessageEnum.DELETEADMINBYPK_FAIL;
        BaseDto dto = new BaseDto(messageEnum);
        return dto;
    }

    /**
     * 删除实体根据示例
     * @return
     */
    public BaseDto deleteAdminByExample(){
        return null;
    }



    public void test(Integer test1,String test2){
        System.out.println(test1 + "" + test2);
    }

}
